package com.bjpowernode.threadpool;

import java.util.concurrent.*;

/**
 * Executor框架结构
 *
 * @author 关注我，抖音搜索：java512
 */
public class Test01 {

    public static void main(String[] args) {
        Thread t = new Thread () { // t 是执行机制
            /**
             * 工作任务
             */
            @Override
            public void run() {
                System.out.println("Runnable任务1");
            }
        };
        t.start();//Thread执行

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Runnable任务2");
            }
        });
        t2.start();//Thread执行

        //基于Executor框架实现线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                5,
                10,
                15,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(5),
                new ThreadPoolExecutor.CallerRunsPolicy());

        threadPoolExecutor.execute(() -> {
            System.out.println("工作任务1");
        });
        //线程池关闭
        threadPoolExecutor.shutdown();

        //Executors是Executor框架的一个工具类，可以非常方便地创建线程池
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.execute(() -> {
            System.out.println("工作任务2");
        });
        //线程池关闭
        executorService.shutdown();
    }
}